import 'package:drive/src/widget/image/cache/widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ImageNavigatorManage extends NavigatorObserver {
   static ImageNavigatorManage instance= ImageNavigatorManage();

   Route? currentRoute;

   final _cache=<Route?,List<CachedNetworkImage>>{};

   void addCached(CachedNetworkImage cachedImage){
     // _cache[currentRoute]!.add(cachedImage);
   }

   void _currentRoute(Route? currentRoute){
     this.currentRoute=currentRoute;
     if(!_cache.containsKey(currentRoute)){
       _cache[currentRoute]=[];
     }
   }

   void _remove(Route route){
     // final list=_cache[route]!;
     // list.forEach((element) {
     //   PaintingBinding.instance?.imageCache?.evict(element);
     // });
     // list.clear();
   }


   @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);

    _currentRoute(route);
  }
  @override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);

    _currentRoute(previousRoute);

    if(route is PageRoute) {
      route.animation?.addStatusListener((status) {
        if(status==AnimationStatus.dismissed) {
          _remove(route);
        }
      });
    }else if(route is PopupRoute){
      route.animation?.addStatusListener((status) {
        if(status==AnimationStatus.dismissed) {
          _remove(route);
        }
      });
    }else{
      _remove(route);
    }

  }



}